package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 言初
 * Date: 2024-09-10
 * Time: 19:45
 */
/*
 数据中心
 通过这个类，来整合数据库操作
 */
public class DataBaseManager {

    // 从spring中拿到现成的对象
    private MetaMapper metaMapper;

    // 针对数据库进行初始化操作
    public void init(){
        // 手动获取到MetaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        if(!checkDBExists()){
            // 数据库不存在就执行建库建表操作
            // 数据库存在就什么都不用做了
            // 先创建一个data目录
            File dataDir = new File("./data");
            dataDir.mkdirs();

            // 创建数据库
            createTable();
            // 插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成！");
        }else{
            System.out.println("[DataBaseManager] 数据库已经存在 ！ ");
        }
    }

    private void createTable() {
        // 创建数据表的操作
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成");
    }

    private boolean checkDBExists() {
        // 判断数据库是否存在的方法，就是判断文件是否存在
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }

    private void createDefaultData() {
        // 插入默认数据
        // 此处主要是为了添加一个默认的交换机，类型为直接交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);

        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始化数据完成 ");
    }

    // 对其他的数据库操作进行封装
    public void insertExchange(Exchange exchange){

        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }

    public String selectArgumentsByQueueName(String queueName){
        return metaMapper.selectArgumentsByQueueName(queueName);
    }

    // 删除数据库文件
    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret){
            System.out.println("[DataBaseManager] 删除数据库文件成功");
        }else{
            System.out.println("[DataBaseManager] 删除数据库文件失败");
        }
        File dataDir = new File("./data");

        ret = dataDir.delete();
        if(ret){
            System.out.println("[DataBaseManager] 删除数据库目录成功");
        }else{
            System.out.println("[DataBaseManager] 删除数据库目录失败");
        }
    }


}
